<template>
  <div class="login-page">
   <van-nav-bar
  title="账号登录"
  left-text="返回"
  left-arrow
  @click-left="onClickLeft"
/>
<van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"
    placeholder="请输入账号"
    :rules="[
    {pattern:/^\w{5}/,message:'用户名至少为5位'}
    ]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    placeholder="请输入密码"
    :rules="[
    {pattern:/^\w{6}/,message:'用户名至少为6位'}
    ]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">登录</van-button>

   <router-link class="link" to="/register">还没有账号,去注册~</router-link>

  </div>
</van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (value) {
      // console.log('submit', value)
      const { body } = await login(value)
      // console.log(body)
      setToken(body.token)
      this.$toast.success('登录成功')
      this.$router.push('/')
    },
    onClickLeft () {
      this.$router.back()
    }
  }
}
</script>

<style lang='less' scoped>
/deep/ .van-nav-bar__content{
  background-color: #21b97a;
 .van-nav-bar__title {
    color: #fff;
  }
 .van-icon-arrow-left:before{
    color: #fff;
  }
}
.link{
  display: block;
  margin-top: 20px;
margin-left:100px;
color: #6f6f6f;

}
</style>
